<?xml version="1.0" encoding="UTF-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>ITERATOR Design Pattern</Title>
      <Author>LSANTANA</Author>
      <Description>Define uma interface nos moldes do framework Dao do siga2.</Description>
      <Shortcut>clRic</Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Object>
          <ID>Classe_De_Dominio</ID>
          <Type>Object</Type>
          <ToolTip>Digite a classe de dominio?</ToolTip>
          <Default>[Classe_de_Dominio]</Default>
        </Object>
        <Object>
          <ID>NAMESPACE</ID>
          <Type>Object</Type>
          <ToolTip>Namespace e Assembly</ToolTip>
          <Default>[NameSpace]</Default>
        </Object>
		<Object>
          <ID>AbstractIterator</ID>
          <Type>Object</Type>
          <ToolTip></ToolTip>
          <Default>[AbstractIterator]</Default>
        </Object>
		<Object>
          <ID>ConcreteIterator</ID>
          <Type>Object</Type>
          <ToolTip></ToolTip>
          <Default>[ConcreteIterator]</Default>
        </Object>
        <Object>
		  <ID>AbstractCollection</ID>
          <Type>Object</Type>
          <ToolTip></ToolTip>
          <Default>[AbstractCollection]</Default>
        </Object>
        <Object>
		  <ID>Collection</ID>
          <Type>Object</Type>
          <ToolTip></ToolTip>
          <Default>[Collection]</Default>
        </Object>
      </Declarations>
      <Code Language="VB" Kind="file"><![CDATA[
'The classes and/or objects participating in this pattern are:
'
'    Iterator  ($AbstractIterator$ ex.: AbstractIterator)
'        defines an interface for accessing and traversing elements.
'    ConcreteIterator  ($ConcreteIterator$ ex.: Iterator)
'        implements the Iterator interface.
'        keeps track of the current position in the traversal of the aggregate.
'    Aggregate  ($AbstractCollection$ ex.: AbstractCollection)
'        defines an interface for creating an Iterator object
'    ConcreteAggregate  ($Collection$ ex.: Collection)
'        implements the Iterator creation interface to return an instance of the proper $ConcreteIterator$

Imports System.Collections

Namespace DoFactory.GangOfFour.Iterator.Structural


    ''' <summary>
    ''' MainApp startup class for Structural
    ''' Iterator Design Pattern.
    ''' </summary>
    Public Class MainApp

        ''' <summary>
        ''' Entry point into console application.
        ''' </summary>
        Public Shared Sub Main()

            Dim a As New $Collection$()
            a(0) = "Item A"
            a(1) = "Item B"
            a(2) = "Item C"
            a(3) = "Item D"

            ' Create Iterator and provide aggregate
            Dim i As New $ConcreteIterator$(a)
            Console.WriteLine("Iterating over collection:")

            Dim item As Object = i.First()
            While item IsNot Nothing
                Console.WriteLine(item)
                item = i.[Next]()
            End While

            ' Wait for user
            'Console.ReadKey()
        End Sub

    End Class

    ''' <summary>
    ''' The 'Aggregate' abstract class
    ''' </summary>
    MustInherit Class $AbstractCollection$
        Public MustOverride Function CreateIterator() As $AbstractIterator$
    End Class

    ''' <summary>
    ''' The 'ConcreteAggregate' class
    ''' </summary>
    Class $Collection$
        Inherits $AbstractCollection$

        Private _items As New ArrayList()

        Public Overrides Function CreateIterator() As $AbstractIterator$
            Return New $ConcreteIterator$(Me)
        End Function

        ' Gets item count
        Public ReadOnly Property Count() As Integer
            Get
                Return _items.Count
            End Get
        End Property

        ' Indexer
        Default Public Property Item(ByVal index As Integer) As Object
            Get
                Return _items(index)
            End Get
            Set(ByVal value As Object)
                _items.Insert(index, value)
            End Set
        End Property

    End Class

    ''' <summary>
    ''' The 'Iterator' abstract class
    ''' </summary>
    MustInherit Class $AbstractIterator$
        Public MustOverride Function First() As Object

        Public MustOverride Function [Next]() As Object

        Public MustOverride Function IsDone() As Boolean

        Public MustOverride Function CurrentItem() As Object

    End Class

    ''' <summary>
    ''' The 'ConcreteIterator' class
    ''' </summary>
    Class $ConcreteIterator$
        Inherits $AbstractIterator$

        Private _aggregate As $Collection$

        Private _current As Integer = 0

        ' Constructor
        Public Sub New(ByVal aggregate As $Collection$)
            Me._aggregate = aggregate
        End Sub

        ' Gets first iteration item
        Public Overrides Function First() As Object
            Return _aggregate(0)
        End Function

        ' Gets next iteration item
        Public Overrides Function [Next]() As Object
            Dim ret As Object = Nothing

            If _current < _aggregate.Count - 1 Then
                ret = _aggregate(System.Threading.Interlocked.Increment(_current))
            End If
            Return ret
        End Function

        ' Gets current iteration item
        Public Overrides Function CurrentItem() As Object
            Return _aggregate(_current)
        End Function

        ' Gets whether iterations are complete
        Public Overrides Function IsDone() As Boolean
            Return _current >= _aggregate.Count
        End Function

    End Class

End Namespace


  
]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>